<?php

namespace app\applet\controller;



use think\Db;
use think\Validate;
class Message
{
    public function __construct()
    {

        $this->message = new \app\applet\model\Message();
    }

    //消息列表
    public function lists()
    {
        $input=input();
        $where=[];
        if(!empty($input['class_type']))
        {
            $where['class_type']=$input['class_type'];
        }
        $member_id = get_header_id("token");
        $where['member_id']=$member_id;
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->message->getMessageList($current_page,$page_num,$where);
        returnJson(200,'success',$data);
    }

    /**
     * 会员商品升级审核
     */
    public function grade(){


        $input = input();
        $validate = new Validate([
            ['m_id', 'require', '消息ID不能为空'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }

        $member_id = get_header_id("token");
        $input['member_id']=$member_id;

        $this->message->examineGrade($input);
        returnJson(200,'success');
    }

    /**
     * 我的消息---页面
     */
    public function myMsg(){
        $member_id = get_header_id("token");
        //$member_id=1;

        $data=$this->message->getNew($member_id);

        returnJson(200,'success',$data);
    }

    /**
     * 我的消息---快速处理--
     *      type  1=全部  2=别人发给我  3=我发给别人
     *   class_type
     */
    public function msgList(){

        $param=input();
        $member_id = get_header_id("token");
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $type=empty($param['type']) ? 1: $param['type'];
        $class_type=empty($param['class_type']) ? 13: $param['class_type'];
        $user_id=$param['user_id'];//发消息给我的人或者我发消息的人
        //$member_id=38;

        $data=$this->message->msgList($type,$page,$page_size,$class_type,$member_id,$user_id);

        returnJson(200,'success',$data);

    }


    /**
     * 我的消息---某个分类下的列表
     */
    public function cateMsgList(){
        $param=input();
        $member_id = get_header_id("token");
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $class_type=empty($param['class_type']) ? 13: $param['class_type'];
        //$member_id=32;

        $data=$this->message->msgClassList($page,$page_size,$class_type,$member_id);

        returnJson(200,'success',$data);

    }


    /**
     * 我的消息---详情----消息已读状态
     */
    public function readMsg(){
        $id=input('m_id');
        $member_id = get_header_id("token");
        $this->message->msgDetail($id,$member_id);

        returnJson(200,'操作成功');
    }


    /**
     * 我的消息---发送消息
     */
    public function sendMsg(){
        $param=input();
        $validate = new Validate([
            ['user_id', 'require', '发送人不能为空'],
            ['title', 'require|max:30', '标题不能为空|标题长度不能超过30'],
            ['content', 'require', '内容不能为空'],
            ['class_type', 'require', '消息分类不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");

        $this->message->sendMsg($member_id,$param);

        returnJson(200,'操作成功');
    }

    /**
     * 系统消息分类列表
     */
    public function msgSystemList(){
        $param=input();
        $member_id = get_header_id("token");
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        //$member_id=32;

        $data=$this->message->msgSystemList($page,$page_size,$member_id);

        returnJson(200,'success',$data);
    }


    /**
     * 系统消息---快速处理--
     */
    public function systemList(){

        $param=input();
        $member_id = get_header_id("token");
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $user_id=$param['user_id'];//发消息给我的人或者我发消息的人
        //$member_id=55;

        $data=$this->message->systemList($page,$page_size,$member_id,$user_id);

        returnJson(200,'success',$data);

    }

}